<template>
	<view class="content">

		<image style="width:100%;position: absolute;bottom:0;left:0;width:100%;" mode="widthFix"
			src="../../static/1.svg"></image>

		<view class="loginBox">
			<h3 style="text-align: center;margin-bottom:120rpx;">欢迎登录</h3>
			<view class="inputBox">
				<view class="ipt">
					<uni-icons type="contact" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="text" v-model="form.username" @blur="validateField('username')" placeholder="请输入账号" />
				</view>
				<view class="ipt">
					<uni-icons type="eye" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="passsword" v-model="form.password" @blur="validateField('password')"
						placeholder="请输入密码" />
				</view>
				<view class="ipt">
					<uni-icons type="checkmarkempty" size="24" color="rgb(66,157,250)"></uni-icons>
					<input type="text" value="" placeholder="请输入验证码" />
					<view class="yzm">
						验证码
					</view>
				</view>
				<text class="error-message" v-if="errors.username">{{ errors.username }}</text>
				<text class="error-message" v-if="errors.password">{{ errors.password }}</text>
				<button @click="login">登录</button>
				<view class="forgetPwd">
					<span>忘记密码</span>
					<span>没有账号，去注册</span>
				</view>
			</view>
			<view class="tipbox">
				<view class="txt">
					—— 其他账号登录 ——
				</view>
				<view class="otherUser">
					<uni-icons type="qq" size="40" color="rgb(66,157,250)"></uni-icons>
					<uni-icons type="weixin" size="40" color="rgb(2,187,17)"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import CONFIG from '../../config.js'
	import http from '../../common/request.js'
	let isPcObserver, isPhoneObserver

	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				errors: {},
				form: {
					username: '',
					password: ''
				},
				token: ''
			}
		},
		methods: {

			// 字段验证
			validateField(field) {
				const value = this.form[field];
				const errors = {};

				if (field === 'username' && !value) {
					errors.username = '用户名不能为空';
				}

				if (field === 'password' && !value) {
					errors.password = '密码不能为空';
				} else if (field === 'password' && value.length < 6) {
					errors.password = '密码长度不能少于6位';
				}

				this.errors = errors;
				return !errors[field];
			},

			// 表单验证
			validateForm() {
				const isValidUsername = this.validateField('username');
				const isValidPassword = this.validateField('password');
				return isValidUsername && isValidPassword;
			},

			login_request() {
				return http.requestWithHeader({
					url: '/api/auth/login',
					method: 'POST',
					data: {
						username: this.form.username,
						password: this.form.password
					}
				}, {
					'Content-Type': 'application/json'
				})
			},

			async login() {

				if (!this.validateForm()) {
					return;
				};
				
				await this.login_request().then((data) => {
						console.log('登录成功: ' + data.data.token);
						uni.setStorageSync('token', data.data.token);
					})
					.catch(error => {
						//console.error('登录失败:', error);
						// 显示错误提示
						uni.showToast({
							title: '登录失败，请检查用户名和密码',
							icon: 'none'
						});
						// return
					});
				
				isPcObserver = uni.createMediaQueryObserver(this)
				isPhoneObserver = uni.createMediaQueryObserver(this)

				isPcObserver.observe({
					minWidth: 768
				}, matched => {
					uni.reLaunch({
						url: '/pages/home/home'
					})
				})
				isPhoneObserver.observe({
					maxWidth: 767
				}, matched => {
					this.isPC = !matched
					if (matched) {
						uni.switchTab({
							url: '/pages/menu/menu',
							success(e) {

							}
						})
					}
				})

			},


		}
	}
</script>

<style scoped>
	svg {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 40%;
		box-sizing: border-box;
		display: block;
		background-color: #ffffff;
	}

	.loginBox {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -60%);
		width: 90%;
		border-radius: 20rpx;
		padding: 60rpx;
		box-sizing: border-box;
	}

	h3 {
		color: rgb(66, 157, 250);
		font-size: 40rpx;
		letter-spacing: 10rpx;
		margin-bottom: 40rpx;
	}

	.inputBox {}

	.ipt {
		height: 86rpx;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-bottom: 40rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
		padding-left: 10rpx;
	}

	.ipt input {
		margin-left: 20rpx;
		font-size: 28rpx;
	}

	.ipt input {
		margin-left: 20rpx;
	}

	.forgetPwd {
		margin-top: 30rpx;
		font-size: 26rpx;
		color: #b5b5b5;
		text-align: end;
		padding: 0 10rpx;
		display: flex;
		justify-content: space-between;
	}

	button {
		margin-top: 20rpx;
		line-height: 85rpx;
		text-align: center;
		background: rgb(66, 157, 250);
		border-radius: 40rpx;
		color: #fff;
		margin-top: 40rpx;
	}

	.tip {
		text-align: center;
		font-size: 28rpx;
		position: fixed;
		bottom: 50rpx;
		left: 50%;
		transform: translate(-50%, -50%);
		color: #f4f4f4;
	}

	.tipbox {
		text-align: center;
		margin-top: 100rpx;
	}

	.otherUser {
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
	}

	.txt {
		font-size: 28rpx;
		color: #cbcbcb;
	}

	.otherUser .uni-icons {
		margin-left: 20rpx;
	}

	.yzm {
		text-align: end;
		font-size: 24rpx;
		background: linear-gradient(to right, rgb(66, 157, 250), rgb(0, 170, 127));
		height: 60rpx;
		width: 150rpx;
		line-height: 60rpx;
		text-align: center;
		border-radius: 10rpx;
		color: #fff;
	}
	
	
</style>